package com.bwie.interrogation.service.impl;

import com.bwie.common.core.exception.ServiceException;
import com.bwie.common.security.utils.SecurityUtils;

import com.bwie.interrogation.domain.Drug;
import com.bwie.interrogation.domain.Prescription;
import com.bwie.interrogation.domain.dto.PrescriptionListLlbDto;
import com.bwie.interrogation.mapper.PatientMapper;
import com.bwie.interrogation.mapper.PrescriptionMapper;
import com.bwie.interrogation.service.IprescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 我的处方Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-10
 */
@Service
public class PrescriptionServiceImpl implements IprescriptionService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private PrescriptionMapper prescriptionMapper;
    /**
     * 查询我的处方
     *
     * @param id 我的处方主键
     * @return 我的处方
     */
    @Override
    public Prescription selectprescriptionById(Long id) {
        return prescriptionMapper.selectprescriptionById(id);
    }

    /**
     * 查询我的处方列表
     *
     * @param prescription 我的处方
     * @return 我的处方
     */
    @Override
    public List<Prescription> selectprescriptionList(Prescription prescription) {
        return prescriptionMapper.selectprescriptionList(prescription);
    }

    /**
     * 修改我的处方
     *
     * @param prescription 我的处方
     * @return 结果
     */
    @Override
    public int updateprescription(Prescription prescription) {
        return prescriptionMapper.updateprescription(prescription);
    }

    /**
     * 批量删除我的处方
     *
     * @param ids 需要删除的我的处方主键
     * @return 结果
     */
    @Override
    public int deleteprescriptionByIds(Long[] ids) {
        return prescriptionMapper.deleteprescriptionByIds(ids);
    }

    /**
     * 删除我的处方信息
     *
     * @param id 我的处方主键
     * @return 结果
     */
    @Override
    public int deleteprescriptionById(Long id) {
        return prescriptionMapper.deleteprescriptionById(id);
    }

    /**
     * 查询药品列表
     */
    @Override
    public List<Drug> prescriptionList() {
        List<Drug> list = prescriptionMapper.prescriptionList();
        return list;
    }

    /**
     * 新增我的处方
     *
     * @param prescription 我的处方
     * @return 结果
     */
    @Override
    public void addPrescription(PrescriptionListLlbDto prescription) {
        //获取处方表
        Prescription prescriptionAdd = prescription.getPrescription();
        // 校验处方信息是否为空
        if(prescription ==null) {
            throw new ServiceException("处方信息不能为空");
        }
        // 校验药品用量信息是否为空
        if (prescription.getDosageList() == null || prescription.getDosageList().isEmpty()) {
            throw new ServiceException("药品用量信息不能为空");
        }
        //获取医生ID
        Long userId = SecurityUtils.getUserId();
        userId = 1L;
        //根据挂号表医生ID获取患者ID
        Long patientId = patientMapper.selectById(userId);
        //赋值患者ID
        prescriptionAdd.setPatientId(patientId);
        //赋值医生ID
        prescriptionAdd.setDoctorId(userId);
        //添加一个我的处方表——返回ID
        Long addPrescriptionId = patientMapper.addPrescription(prescriptionAdd);

        //添加多个药品用量表——返回ID
        prescription.getDosageList().forEach(dosage -> {
            //添加多个药品用量表
            Long addDosageId = patientMapper.addDosage(dosage);
            //添加处方中间表
            patientMapper.addPrescriptionDosage(addPrescriptionId, addDosageId);
        });
    }
}
